<template>
  <div style="display: flex; justify-content: space-between">
    <div class="camera_outer" style="width: 50%; border-radius: 10px;background: '#587ab3'">
      <!-- <video
        id="videoCamera"
        :width="videoWidth"
        :height="videoHeight"
        autoplay
      ></video>
      <canvas
        style="display: none"
        id="canvasCamera"
        :width="videoWidth"
        :height="videoHeight"
      ></canvas>
      <div class="button">
        <el-button @click="getCompetence()" type="primary"
          >打开摄像头</el-button
        >
        <el-button @click="stopNavigator()" type="warning"
          >关闭摄像头</el-button
        >
        <el-button @click="setImage()" v-if="photograph" type="success"
          >拍照</el-button
        >
      </div> -->
      <div class="img_bg_camera" style="width: 100%; height: 100%;background: '#587ab3'">
        <!-- <div style="display: flex; align-items: center">
          <p style="width: 80px">用户名:</p>
          <el-input v-model="username"></el-input>
        </div> -->
        <!-- <img :src="imgSrc" alt class="tx_img" /> -->
        <img src="" alt class="tx_img" style="width: 100%; height: 50%" />
        <div style="width: 100%; display: flex; justify-content: space-between">
          <el-button @click="getCompetence()" type="primary"
            >登录拍照进行验证是否当前工序人员</el-button
          >
          <el-button @click="stopNavigator()" type="warning"
            >退出清除当前工序人员</el-button
          >
        </div>
        <!-- <el-button @click="addNew()" type="warning" style="padding-top: 10px"
          >新建</el-button
        > -->
      </div>
    </div>
    <div
      class="home_table"
      style="
        height: calc(100vh - 84px);
        flex: 1;
        padding-left: 10px;
        width: 30%;
        background: '#445e89e6'
      "
    >
      <el-table
        :data="tableData"
        height="100%"
        border
        :header-cell-style="{
          textAlign: 'center',
          background: '#445e89e6',
          color: '#ffffff',
        }"
        style="width: 100%; border-radius: 10px; border: 0"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column prop="workstation" label="工位"></el-table-column>
        <el-table-column
          prop="personnelCode"
          label="齿轮编号"
        ></el-table-column>
        <el-table-column prop="personnelName" label="人员"></el-table-column>
        <el-table-column prop="updateTime" label="时间"></el-table-column>

        <!-- <el-table-column prop="postName" label="岗位名称"></el-table-column>
        <el-table-column prop="updateTime" label="修改日期"></el-table-column>
        <el-table-column prop="updateBy" label="修改人"></el-table-column> -->
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      videoWidth: 500,
      videoHeight: 300,
      video: true,
      photograph: false,
      imgSrc: "",
      thisCancas: null,
      thisContext: null,
      thisVideo: null,
      openVideo: false,
      // 渲染表格数据
      tableData: [],
      form: [
        {
          equipmentCode: "2",
          equipmentModel: "1",
          equipmentName: "扫码枪",
          equipmentStatus: "在 线",
          equipmentType: "1",
          personnelCode: "2",
          personnelName: "2",
          postName: "2",
          postStatus: "在 线",
          status: "在 线",
          time: "2024-05-17 00:00:00",
          updateBy: "1",
          updateTime: "2024-05-09 18:07:18",
          user: "1",
          workstation: "2",
        },
      ],
    };
  },
  created() {
    this.showList();
  },
  methods: {
    // 渲染数据
    showList() {
      if (JSON.parse(window.localStorage.getItem("record"))) {
        this.record = JSON.parse(window.localStorage.getItem("record"));
        this.tableData = [...this.record]; //渲染数组
        this.total = this.record.length; //数据总条数
        this.changePage(this.record); //进入页面返回到第一页
      }
    },
    getCompetence() {
      this.photograph = true;
      var _this = this;
      _this.thisCancas = document.getElementById("canvasCamera");
      _this.thisContext = this.thisCancas.getContext("2d");
      _this.thisVideo = document.getElementById("videoCamera");
      _this.thisVideo.style.display = "block";
      // 获取媒体属性，旧版本浏览器可能不支持mediaDevices，我们首先设置一个空对象
      if (navigator.mediaDevices === undefined) {
        navigator.mediaDevices = {};
      }
      // 一些浏览器实现了部分mediaDevices，我们不能只分配一个对象
      // 使用getUserMedia，因为它会覆盖现有的属性。
      // 这里，如果缺少getUserMedia属性，就添加它。
      if (navigator.mediaDevices.getUserMedia === undefined) {
        navigator.mediaDevices.getUserMedia = function (constraints) {
          // 首先获取现存的getUserMedia(如果存在)
          var getUserMedia =
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.getUserMedia;
          // 有些浏览器不支持，会返回错误信息
          // 保持接口一致
          if (!getUserMedia) {
            //不存在则报错
            return Promise.reject(
              new Error("getUserMedia is not implemented in this browser")
            );
          }
          // 否则，使用Promise将调用包装到旧的navigator.getUserMedia
          return new Promise(function (resolve, reject) {
            getUserMedia.call(navigator, constraints, resolve, reject);
          });
        };
      }
      var constraints = {
        audio: false,
        video: {
          width: this.videoWidth,
          height: this.videoHeight,
          transform: "scaleX(-1)",
        },
      };
      navigator.mediaDevices
        .getUserMedia(constraints)
        .then(function (stream) {
          // 旧的浏览器可能没有srcObject
          if ("srcObject" in _this.thisVideo) {
            _this.thisVideo.srcObject = stream;
          } else {
            // 避免在新的浏览器中使用它，因为它正在被弃用。
            _this.thisVideo.src = window.URL.createObjectURL(stream);
          }
          _this.thisVideo.onloadedmetadata = function (e) {
            _this.thisVideo.play();
          };
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //  绘制图片（拍照功能）
    setImage() {
      var _this = this;
      // canvas画图
      _this.thisContext.drawImage(
        _this.thisVideo,
        0,
        0,
        _this.videoWidth,
        _this.videoHeight
      );
      // 获取图片base64链接
      var image = this.thisCancas.toDataURL("image/png");
      _this.imgSrc = image; //赋值并预览图片
    },
    // 关闭摄像头
    stopNavigator() {
      this.thisVideo.srcObject.getTracks()[0].stop();
      this.video = false;
      this.photograph = false;
    },
    // base64转文件，此处没用到
    dataURLtoFile(dataurl, filename) {
      var arr = dataurl.split(",");
      var mime = arr[0].match(/:(.*?);/)[1];
      var bstr = atob(arr[1]);
      var n = bstr.length;
      var u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], filename, { type: mime });
    },
    // 增加
    addNew() {
      this.record.unshift(this.form);
      this.record.sort((a, b) => {
        return (
          new Date(b.updateTime).getTime() - new Date(a.updateTime).getTime()
        );
      }); //降序
      window.localStorage.setItem("record", JSON.stringify(this.record));
      this.form = {};
      this.dialogFormVisible = false;
      this.showList();
    },
    //选中数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
  },
};
</script>

<style lang="scss">
.camera_outer {
  // width: 31.25rem;
  // width: 500px;
  // background-color: rgba(56, 93, 138);
  // background-color: white;
  background-color: #445e89e6;
  padding: 10px;
  .button {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding-top: 10px;
  }
}
.rowStyle {
  color: rgb(28, 235, 211) !important;
  background: rgb(36 52 93) !important;
}
.el-table__body-wrapper {
  background-color: #445e89e6;
}
.el-table td.el-table__cell,
.el-table th.el-table__cell.is-leaf {
  // border: 0;
  border: 1px solid #587ab3;
}

.el-table::before {
  height: 0px;
}
/* 悬停划过高亮，鼠标经过，当前行的颜色 */
.el-table__body tr:hover > td {
  background-color: rgb(36 52 93) !important;
  color: rgb(28, 235, 211);
}
.el-table--border,
.el-table--group {
  border: 0px solid #ebeef5;
}
.home_table .el-table__body tr:nth-child(even) {
  background-color: #3c5276;
  color: white;
}
.home_table .el-table__body tr:nth-child(odd) {
  background-color: #445e89;
  color: white;
}
.el-table--border::after, .el-table--group::after, .el-table::before {
    content: '';
    position: absolute;
    /* background-color: #EBEEF5; */
    background-color: #445e89; 
    z-index: 1;
}
</style>
